package models

import (
	"gorm.io/gorm"
	"time"
)

// OperationLog 操作日志表
type OperationLog struct {
	ID            uint64         `gorm:"primaryKey;autoIncrement;comment:主键ID" json:"id"`
	LogType       string         `gorm:"type:varchar(20);not null;index;comment:日志类型(operation-操作,error-错误,audit-审计,security-安全)" json:"log_type"`
	Module        string         `gorm:"type:varchar(50);not null;index;comment:模块名称(order-订单,user-用户,payment-支付,contract-合同)" json:"module"`
	Operation     string         `gorm:"type:varchar(50);not null;index;comment:操作类型(create-创建,update-更新,delete-删除,query-查询,login-登录,logout-登出)" json:"operation"`
	ResourceType  string         `gorm:"type:varchar(50);not null;comment:资源类型(order-订单,user-用户,payment-支付等)" json:"resource_type"`
	ResourceID    string         `gorm:"type:varchar(64);not null;index;comment:资源ID" json:"resource_id"`
	ResourceName  string         `gorm:"type:varchar(200);comment:资源名称" json:"resource_name"`
	UserID        string         `gorm:"type:varchar(64);not null;index;comment:操作用户ID" json:"user_id"`
	UserName      string         `gorm:"type:varchar(100);comment:操作用户名称" json:"user_name"`
	UserType      string         `gorm:"type:varchar(20);comment:用户类型(admin-管理员,agent-代理商,landlord-房东,tenant-租客)" json:"user_type"`
	IPAddress     string         `gorm:"type:varchar(45);comment:IP地址" json:"ip_address"`
	UserAgent     string         `gorm:"type:text;comment:用户代理" json:"user_agent"`
	RequestMethod string         `gorm:"type:varchar(10);comment:请求方法(GET,POST,PUT,DELETE)" json:"request_method"`
	RequestURL    string         `gorm:"type:varchar(500);comment:请求URL" json:"request_url"`
	RequestParams string         `gorm:"type:text;comment:请求参数" json:"request_params"`
	ResponseCode  int32          `gorm:"comment:响应状态码" json:"response_code"`
	ResponseTime  int64          `gorm:"comment:响应时间(毫秒)" json:"response_time"`
	OldData       string         `gorm:"type:text;comment:变更前数据" json:"old_data"`
	NewData       string         `gorm:"type:text;comment:变更后数据" json:"new_data"`
	ChangeFields  string         `gorm:"type:text;comment:变更字段列表" json:"change_fields"`
	Result        string         `gorm:"type:varchar(20);default:success;comment:操作结果(success-成功,failed-失败,error-错误)" json:"result"`
	ErrorMessage  string         `gorm:"type:text;comment:错误信息" json:"error_message"`
	Description   string         `gorm:"type:text;comment:操作描述" json:"description"`
	ExtraData     string         `gorm:"type:text;comment:扩展数据" json:"extra_data"`
	CreatedAt     time.Time      `gorm:"autoCreateTime;index;comment:创建时间" json:"created_at"`
	UpdatedAt     time.Time      `gorm:"autoUpdateTime;comment:更新时间" json:"updated_at"`
	DeletedAt     gorm.DeletedAt `gorm:"index;comment:软删除时间" json:"deleted_at,omitempty"`
}

// LogType 日志类型常量
const (
	LogTypeOperation = "operation" // 操作日志
	LogTypeError     = "error"     // 错误日志
	LogTypeAudit     = "audit"     // 审计日志
	LogTypeSecurity  = "security"  // 安全日志
)

// Module 模块常量
const (
	ModuleOrder    = "order"    // 订单模块
	ModuleUser     = "user"     // 用户模块
	ModulePayment  = "payment"  // 支付模块
	ModuleContract = "contract" // 合同模块
	ModuleSystem   = "system"   // 系统模块
)

// Operation 操作类型常量
const (
	OperationCreate    = "create"    // 创建
	OperationUpdate    = "update"    // 更新
	OperationDelete    = "delete"    // 删除
	OperationQuery     = "query"     // 查询
	OperationLogin     = "login"     // 登录
	OperationLogout    = "logout"    // 登出
	OperationAccept    = "accept"    // 接单
	OperationReject    = "reject"    // 拒绝
	OperationAssign    = "assign"    // 分配
	OperationStart     = "start"     // 开始
	OperationComplete  = "complete"  // 完成
	OperationCancel    = "cancel"    // 取消
	OperationSuspend   = "suspend"   // 暂停
	OperationResume    = "resume"    // 恢复
	OperationTerminate = "terminate" // 终止
	OperationReopen    = "reopen"    // 重新开启
)

// ResourceType 资源类型常量
const (
	ResourceTypeOrder    = "order"    // 订单
	ResourceTypeUser     = "user"     // 用户
	ResourceTypePayment  = "payment"  // 支付
	ResourceTypeContract = "contract" // 合同
	ResourceTypeOperator = "operator" // 操作员
)

// UserType 用户类型常量
const (
	UserTypeAdmin    = "admin"    // 管理员
	UserTypeAgent    = "agent"    // 代理商
	UserTypeLandlord = "landlord" // 房东
	UserTypeTenant   = "tenant"   // 租客
	UserTypeSystem   = "system"   // 系统
)

// Result 操作结果常量
const (
	ResultSuccess = "success" // 成功
	ResultFailed  = "failed"  // 失败
	ResultError   = "error"   // 错误
)
